package com.ruoyi.yuce.controller;

import com.ruoyi.common.annotation.DataSource;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.enums.DataSourceType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.yuce.domain.YcDataCeshi;
import com.ruoyi.yuce.domain.pojo.CoBuJian;
import com.ruoyi.yuce.domain.pojo.CoBuJianCanShu;
import com.ruoyi.yuce.domain.pojo.CoBuJianLeiXing;
import com.ruoyi.yuce.domain.pojo.CoChangZhan;
import com.ruoyi.yuce.service.*;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.security.access.prepost.PreAuthorize;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletResponse;
import java.sql.Date;
import java.text.SimpleDateFormat;
import java.util.List;

/**
 * 遥测记录Controller

 */
@RestController
@RequestMapping("/ruoyi-yuce/ycdataceshi")
@DataSource(value = DataSourceType.SLAVELS)
public class YcDataCeshiController extends BaseController
{
    @Autowired
    private IYcDataCeshiService ycDataService;

    @Autowired
    private IChangzhanService changzhanService;
    @Autowired
    private IBujianleixingService bujianleixingService;
    @Autowired
    private IBujiancanshuService bujiancanshuService;
    @Autowired
    private IFengjiService fengjiService;

    @Autowired
    private INibianqiService nibianqiService;

    /**
     * 查询遥测记录列表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:ycdataceshi:list')")
    @GetMapping("/list")
    public TableDataInfo list(YcDataCeshi ycDataCeshi) {

        startPage();
        Date date = Date.valueOf(ycDataCeshi.getCriqi());
        SimpleDateFormat sfb = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String biao="ls.ycdata"+sfb.format(date);
        String time=sf.format(date);
        ycDataCeshi.setBriqi(biao);
        ycDataCeshi.setCriqi(time);
        List<YcDataCeshi> list = ycDataService.selectYcDataList(ycDataCeshi);
        return getDataTable(list);
    }

    /**
     * 查询部件
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:ycdataceshi:getBuJian')")
    @GetMapping("/getBuJian")
    public List<CoBuJian> getBuJian(YcDataCeshi ycDataCeshi) {
        logger.info("getBuJian function ...");
        int n = ycDataCeshi.getBujianleixingid();

        List<CoBuJian> buJian = null;
        if (n== 49){
            buJian = fengjiService.getBuJian();
        }else if(n == 45){
            buJian = nibianqiService.getBuJian();
        }
        return buJian;
    }

    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:ycdataceshi:getBuJian1')")
    @GetMapping("/getBuJian1")
    public List<CoBuJian> getBuJian1(YcDataCeshi ycDataCeshi) {
        logger.info("getBuJian function ...");
        int n = ycDataCeshi.getBujianleixingid();
        int czid = ycDataCeshi.getChangzhanid();
        List<CoBuJian> buJian = null;
        if (n== 49){
            buJian = fengjiService.getBuJian1(czid);
        }else if(n == 45){
            buJian = nibianqiService.getBuJian();
        }
        return buJian;
    }

    /**
     * 查询场站名
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:ycdataceshi:getChangZhanName')")
    @GetMapping("/getChangZhanName")
    public List<CoChangZhan> getChangZhanName()
    {
        logger.info("getChangZhanName function ...");
        List<CoChangZhan> changZhanName = changzhanService.getChangZhanName();
        return changZhanName;
    }

    /**
     * 查询部件类型名
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:ycdataceshi:getBuJianLeiXing')")
    @GetMapping("/getBuJianLeiXing")
    public List<CoBuJianLeiXing> getBuJianLeiXing()
    {
        logger.info("getBuJianLeiXing function ...");
        List<CoBuJianLeiXing> buJianLeiXing = bujianleixingService.getBuJianLeiXing();
        return buJianLeiXing;
    }

    /**
     * 查询部件参数名
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:ycdataceshi:getBuJianCanShu')")
    @GetMapping("/getBuJianCanShu")
    public List<CoBuJianCanShu> getBuJianCanShu()
    {
        logger.info("getBuJianCanShu function ...");
        List<CoBuJianCanShu> buJianCanShu = bujiancanshuService.getBuJianCanShuName();
        return buJianCanShu;
    }

    /**
     * 查询逆变器部件
     */
    @DataSource(value = DataSourceType.SLAVE)
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:ycdataceshi:getNiBianQi')")
    @GetMapping("/getNiBianQi")
    public List<CoBuJian> getNiBianQi() {
        logger.info("getNiBianQi function ...");
        List<CoBuJian> buJian = nibianqiService.getBuJian();
        return buJian;
    }


    /**
     * 导出遥测记录表
     */
    @PreAuthorize("@ss.hasPermi('ruoyi-yuce:ycdataceshi:export')")
    @Log(title = "遥测记录表", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    public void export(HttpServletResponse response, YcDataCeshi ycDataCeshi)
    {
        Date date = Date.valueOf(ycDataCeshi.getCriqi());
        SimpleDateFormat sfb = new SimpleDateFormat("yyyyMM");
        SimpleDateFormat sf = new SimpleDateFormat("yyyy-MM-dd");
        String biao="ls.ycdata"+sfb.format(date);
        String time=sf.format(date);
        ycDataCeshi.setBriqi(biao);
        ycDataCeshi.setCriqi(time);
        List<YcDataCeshi> list = ycDataService.selectYcDataList(ycDataCeshi);
        ExcelUtil<YcDataCeshi> util = new ExcelUtil<YcDataCeshi>(YcDataCeshi.class);
        util.exportExcel(response, list, "遥测记录表");
    }


}
